﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KF.DHLModel;

namespace KF.DHLBL
{
    public class Prcing
    {
        public double CalcPrice(List<KF.DHLModel.PriceInfo> priceInfos, int zone, double weight, ItemType itemType, CustomerType customerType)
        {
            double pricingWeight = Math.Ceiling(weight * 2) * 0.5;//取到0.5的整数

            #region 普通包裹
            //if (itemType == ItemType.Normal)
            {
                //价格类型 临界值
                var priceTypeInfo = (from info in priceInfos
                                     where info.Zone == zone
                                        && info.PriceType == PriceType.Unit
                                        && info.ItemType == itemType //包裹类型
                                        && info.Type == customerType
                                     orderby info.StartWeight ascending
                                     select info).First();

                if (priceTypeInfo.StartWeight > pricingWeight)
                {

                    var basicPriceInfos = from info in priceInfos
                                          where info.Zone == zone //区域
                                              && pricingWeight >= info.StartWeight //阶梯价 
                                              && info.ItemType == itemType //包裹类型
                                              && info.Type == customerType //报价类型
                                          orderby info.StartWeight ascending
                                          select info;

                    double sumPrice = 0.0d;
                    for (int i = 0; i < basicPriceInfos.Count(); ++i)
                    {
                        if (i == 0)
                        {
                            double price = (basicPriceInfos.ElementAt(i).EndWeight  -basicPriceInfos.ElementAt(i).StartWeight) * 2 * basicPriceInfos.ElementAt(i).Price + basicPriceInfos.ElementAt(i).Price;
                            sumPrice += price;
                        }
                        else if (i < basicPriceInfos.Count() - 1)
                        {
                            double price = (basicPriceInfos.ElementAt(i).EndWeight - basicPriceInfos.ElementAt(i).StartWeight) * 2 * basicPriceInfos.ElementAt(i).Price + basicPriceInfos.ElementAt(i).Price;
                            sumPrice += price;
                        }
                        else 
                        {
                            double price = (pricingWeight - basicPriceInfos.ElementAt(i).StartWeight) * 2 * basicPriceInfos.ElementAt(i).Price + basicPriceInfos.ElementAt(i).Price;
                            sumPrice += price;
                        }
                    }
                    return sumPrice;
                }
                else
                {
                    if (itemType == ItemType.NO)
                    {
                        pricingWeight = Math.Ceiling(pricingWeight);//取到1的整数
                    }

                    try
                    {
                        var calcPrice = (from info in priceInfos
                                         where info.Zone == zone
                                            && info.PriceType == PriceType.Unit
                                            && info.ItemType == itemType //包裹类型
                                            && info.Type == customerType
                                            && pricingWeight >= info.StartWeight //阶梯价 
                                            && pricingWeight <= info.EndWeight //阶梯价 
                                         orderby info.StartWeight ascending
                                         select info).First();

                        if (itemType == ItemType.NO)
                        {
                            return (pricingWeight) * calcPrice.Price;
                        }
                        else
                        {
                            return  calcPrice.Price;
                        }

                        
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }


                }
            #endregion

            }

            return 0;
        }

    }
}
